package com.ruoyi.file.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.file.service.ISysFileService;
import com.ruoyi.file.service.OSSService;
import com.ruoyi.system.api.domain.PathVo;
import com.ruoyi.system.api.domain.SysFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * 文件请求处理
 *
 * @author ruoyi
 */
@RestController
public class SysFileController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;


    @Autowired
    public OSSService ossService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);

            String suffix = ".png";
            try {
                suffix = file.getOriginalFilename()
                        .substring(file.getOriginalFilename().lastIndexOf("."));
            } catch (Exception e) {

            }

            // 使用UUID生成文件名称
            String uuidFileName = UUID.randomUUID() + suffix;
            try {
                uuidFileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) +"-"+ DateUtils.dateTimeNow() + file.getOriginalFilename()
                        .substring(file.getOriginalFilename().lastIndexOf("."));
            } catch (Exception e) {

            }

            StringBuffer rootPath = new StringBuffer();
            rootPath.append(DateUtils.getDate());
            rootPath.append("/");
            rootPath.append(uuidFileName);
            String path = ossService.ossByteUpload(rootPath.toString(), file.getBytes());


            SysFile sysFile = new SysFile();
            sysFile.setName(uuidFileName);
            sysFile.setUrl(path);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    public R<String> delete(List<String> paths) {
        System.out.println(paths);
        if (paths != null && paths.size() != 0) {
            int count = paths.size();
//            List<String> list = Arrays.asList(paths);
//            String s = ossService.ossBatchDelete(list);
            for (String path : paths) {
                if (path.equals("http://oss.xunxinsoft.com/img/yi-space-deafult.png")) {
                    count--;
                } else {
                    String s = ossService.ossSingleDelete(path);
                    if (s != null && s.equals("204")) {
                        count--;
                    }
                }
            }
        }
        return R.ok();
    }

    @PostMapping("delete")
    public R<String> delete(@RequestBody PathVo paths) {
        System.out.println(paths);
        if (paths != null && paths.getPaths() != null && paths.getPaths().size() != 0) {
            int count = paths.getPaths().size();
//            List<String> list = Arrays.asList(paths);
//            String s = ossService.ossBatchDelete(list);
            for (String path : paths.getPaths()) {
                if (path.equals("http://oss.xunxinsoft.com/img/yi-space-deafult.png")) {
                    count--;
                } else {
                    String s = ossService.ossSingleDelete(path);
                    if (s != null && s.equals("204")) {
                        count--;
                    }
                }
            }
        }
        return R.ok();
    }
}
